<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">修改员工</block>
		</cu-custom>

		<form id="formEmplotee" @submit="formSubmit">
			<view class="cu-form-group margin-top">
				<view class="title">员工编号</view>
				<input v-model="employeeInfo.staffno" placeholder="请输入员工编号" name="employid"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">员工姓名</view>
				<input v-model="employeeInfo.personname" placeholder="请输入员工姓名" name="employname"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">员工手机</view>
				<input v-model="employeeInfo.personphoneno" placeholder="请输入员工手机号" name="employphone"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">员工身份证</view>
				<input v-model="employeeInfo.idcard"  readonly="readonly" placeholder="请输入员工身份证" name="idcard"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">选择部门</view>
				<picker name="employdepart" @change="PickerChange" :value="index" :range="departlist" range-key="departname">
					<view class="picker">
						{{index>-1?departlist[index].departname:'选择部门'}}
					</view>
				</picker>
			</view>
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					选择人脸照片
				</view>
				<view class="action">
					0/1
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
						<image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="padding">
				<button :disabled="loadding" style="background-color: #5599FF;" :loading="loadding" type="primary" form-type="submit">修改</button>
				<button v-show="false" ref="resrtBtn" style="background-color: #5599FF;" type="default" form-type="reset">重置</button>
			</view>
		</form>

	</view>
</template>

<script>
	var _self;
	var globalData = null;
	export default {
		data() {
			return {
				index: -1,
				departlist: [],
				imgList: [],
				loadding: false,
				employeeInfo: {}
			}
		},
		onShow() {
			var pages = getCurrentPages();
			var currPage = pages[pages.length - 1]
			if (typeof(currPage.imgurl) != 'undefined' && currPage.imgurl != '') {
				this.$set(this.imgList, 0, currPage.imgurl);
			}
		},
		onLoad(options) {
			_self = this;
			globalData = getApp().globalData
			_self.getDepartList()
			if (typeof(options.id) != 'undefined') {
				console.log(options.id)
				_self.getEmployee(options.id)
			}
		},
		methods: {
			getEmployee(id) {
				uni.showLoading({
					title: '加载中'
				})
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_EMPLOYEE_LIST_PAGE,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: globalData.username,
						unitnos: globalData.orgcode,
						token: globalData.token,
						page: 1,
						rows: 1,
						id: id
					},
					success: (res) => {
						var result = res.data.data.results
						console.log(result)
						_self.employeeInfo = result[0]
						//选择出部门
						if (_self.departlist.length > 0) {
							_self.departlist.forEach(function(item, index) {
								if (_self.employeeInfo.depart.orgCode == item.org_code) {
									_self.index = index
								}
							})
						}
						//设置好照片
						this.$set(this.imgList, 0, this.$constant.IMG_VERSION == this.$constant.OSS?_self.employeeInfo.personphoto:this.$constant.BASEURL+_self.employeeInfo.personphoto);
					},
					complete() {
						uni.hideLoading()
					}
				});
			},
			getDepartList() {
				uni.request({
					url: _self.$constant.BASEURL + _self.$constant.GET_DEPARTMENT_LIST,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: globalData.username,
						token: globalData.token,
						orgcode: globalData.orgcode,
					},
					success: function(res) {
						var result = res.data.data
						// console.log(result)
						let departlist = res.data.data.departlist //公司部门树
						_self.departlist = departlist
						_self.index = 0
					},
					fail: () => {
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						})
					},
				});
			},
			PickerChange(e) {
				_self.index = e.detail.value
			},
			ChooseImage() {
				uni.navigateTo({
					url: '../../camera/camera'
				})
			},
			ViewImage(e) {
				uni.previewImage({
					urls: _self.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '你好',
					content: '确定要删除这张照片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							_self.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			checkPhone(number) {
				return /^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/.test(number)
			},
			formSubmit(e) {
				var res = e.detail.value
				if (res.employname == '') {
					uni.showToast({
						title: '请输入员工姓名',
						icon: 'none'
					})
					return
				}

				if (res.employphone == '') {
					uni.showToast({
						title: '请输入员工手机号',
						icon: 'none'
					})
					return
				}

				if (!_self.checkPhone(res.employphone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}

				if (!_self.imgList.length > 0) {
					uni.showToast({
						title: '请选择人脸照片',
						icon: 'none'
					})
					return
				}
				// console.log(res)
				let filePath = ''
				if (_self.imgList[0] != _self.employeeInfo.personphoto) {
					console.log('filePath', filePath)
					filePath = _self.imgList[0]
				}
				_self.loadding = true
				uni.uploadFile({
					url: _self.$constant.BASEURL + _self.$constant.ADD_EMPLOYEE,
					filePath: filePath,
					name: 'personphotos',
					formData: {
						id: _self.employeeInfo.id,
						username: globalData.username,
						token: globalData.token,
						unitno: _self.departlist[res.employdepart].org_code,
						staffno: res.employid,
						personphoneno: res.employphone,
						cardid:res.idcard,
						personname: res.employname,
						source: '微信公众号',
					},
					success: (res) => {
						console.log(res)
						var result = JSON.parse(res.data.toString())
						console.log(res)
						if (result.code == 100) {
							uni.showToast({
								title: '修改成功',
							})
							//清空form表单
							_self.$refs.resrtBtn.$dispatch('Form', 'uni-form-reset', {
								type: 'reset'
							})
							_self.imgList = []
						} else {
							uni.showToast({
								title: result.message,
								icon: 'none'
							})
						}
					},
					complete() {
						_self.loadding = false
					}
				});
			}
		}
	}
</script>

<style>

</style>
